<?php

/**
 * Main Graphical user interface elements
 * 
 * @author Aurelijus Banelis
 */
class MainDesign {
    /*
     * Main elements
     */

    /**
     * HTML code of page header
     */
    public static function header($title) {
        $title = htmlspecialchars($title);
        //FIXME: normal GUI path evaluation
        if (strpos($_SERVER['REQUEST_URI'], '/GUI/') !== false) {
            $prefix = '';
        } else {
            $prefix = 'GUI/Design/';
            $js_prefix = 'GUI/Functions/';
        }
        echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
        ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="lt" lang="lt">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
                <link rel="stylesheet" href="<?=$prefix;?>main.css" type="text/css"/>
                <script src="<?=$js_prefix;?>table.js" type="text/javascript"></script>
                <title><?=$title;?></title>
            </head>
            <body><div class="box">
        <?php
    }
    
    /**
     * HTML of tab header
     */
    public static function tabHeader($name, $command, $selected = false) {
        $name = htmlspecialchars($name);
        $command = htmlspecialchars($command);
        if ($selected) {
            $class = 'active_tab';
        } else {
            $class = 'tab';
        }
        ?> <a href="<?= $command; ?>" class="<?= $class; ?>"><?= $name; ?></a> <?php
        }

        /* Html of all messages */

        public static function messages($type) {
            global $error_messages;
            global $warning_messages;
            global $success_messages;
            switch ($type) {
                case 'error':
                    $messages = $error_messages;
                    if (count($messages) == 1) {
                        $title = "Klaida";
                    } else {
                        $title = "Klaidos";
                    }
                    break;
                case 'warning':
                    $messages = $warning_messages;
                    if (count($messages) == 1) {
                        $title = "Perspėjimas";
                    } else {
                        $title = "Perspėjimai";
                    }
                    break;
                case 'success' :
                    $messages = $success_messages;
                    $title = "";
                    break;
                default: return;
            }
            if (count($messages) == 0) {
                return;
            }
            if ($title != '') {
                $title = '<div class="msg_title">' . $title . ':</div>';
            }
        ?><div class="messages <?= $type; ?>"><?= $title; ?><ul>
        <?php
        foreach ($messages as $msg) {
            ?><li><?= $msg; ?></li><?php
        }
        ?>
            </ul></div><?php
    }

    
    /*
     * Menu in tabs
     */

    /**
     * HTML for left menu in tab content
     */
    public static function leftSubmenuBeginning($title) {
        global $sidebar_content;
        $title = htmlspecialchars($title);
        $sidebar_content .= "<div class='menu'><h2>$title</h2>";
    }

    /**
     * HTML for left menu in tab content
     */
    public static function leftSubmenuEnding() {
        global $sidebar_content;
        $sidebar_content .= '</div>';
    }

    public static function viewElementsList() {
        global $sidebar_content;
        $sidebar_content .= '<div class="viewElementsList"></div>
        <script type="text/javascript">
            $(document).ready(function() {
                initElementsShow("viewElementsList", "shownElement");
            });
        </script>';
    }
    
    /*
     * Content
     */

    public static function h1($text) {
        $text = htmlspecialchars($text);
        echo "<h1>$text</h1>";
    }

    /**
     * @param type $title       title of table or diagram
     * @param type $short_name  name to show in left menu checkbox list
     * @param type $id          HTML id tag
     */
    public static function viewSectionBegin($title, $short_name = null, $id = null) {
        global $last_id;
        $title = htmlspecialchars($title);
        if (!$short_name) {
            $short_name = $title;
        } else {
            $short_name = htmlspecialchars($short_name);
        }
        if ($id) {
            $id = htmlspecialchars($id);
        } else {
            $id = 'view_section_' . (++$last_id);
        }
        echo "<div class='shownElement' id='$id' for='$short_name'>";
        echo "<h2>$title</h2>";
    }

    public static function viewSectionEnd() {
        ?>
        <div><button onclick="showHide(this.parentNode.parentNode)">(Ne)Rodyti visame ekrane</button></div>
        <?
        echo '</div>';
    }
    
    public static function flash($data, $width, $height, $wmode = "transparent", $type = "application/x-shockwave-flash") {
        $txt = '<object type="' . $type . '" data="' . $data . '" width="' . $width . '" height="' . $height . '"><param name="movie" value="' . $data . '"/><param name="wmode" value="' . $wmode. '"/></object>';
        echo $txt;
    }
}